package com.cloud.custom.tasklist.web;

import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.cloud.custom.common.consts.EnumNameConst;
import com.cloud.custom.common.result.Quokka;
import com.cloud.custom.common.result.SelectItem;
import com.cloud.custom.common.utils.MediaStorageHelper;
import com.cloud.custom.common.utils.WordHelper;
import com.cloud.custom.interfaces.FeignClientSystem;
import com.cloud.custom.tasklist.entity.TaskListMedia;
import com.cloud.custom.tasklist.service.TaskListTabMediaService;


/**
 * 多媒体面板相关逻辑
 * 
 * @author Minglong.tao
 *
 */
@RestController
@RequestMapping("/custom/api/tab/media")
public class TaskListTabMediaController {

	@Autowired
	private TaskListTabMediaService recMediaService;

	@Autowired
	private FeignClientSystem feignClientSystem;

	/**
	 * 附件分组
	 * 
	 * @return
	 */
	@GetMapping("catalog")
	public Quokka listMediaCatalog() {
		List<SelectItem> items = feignClientSystem.loadDictItems(EnumNameConst.MEDIA_CATALOG_TYPES);
		return new Quokka(true, items);
	}

	/**
	 * 获取附件树
	 * 
	 * @param recId
	 * @return
	 */
	@GetMapping("listtree")
	public Quokka listMediaTree(long recId, Integer bizId) {
		return new Quokka(true, recMediaService.listMediaTree(recId, bizId));
	}

	/**
	 * 附件上传
	 * 
	 * @param files
	 * @return
	 */
	@PostMapping("upload")
	public Quokka uploadMedia(MultipartFile[] files, TaskListMedia media) {
		recMediaService.uploadMedias(files, media);
		return new Quokka(true);
	}

	/**
	 * 附件删除
	 * 
	 * @param files
	 * @return
	 */
	@GetMapping("delete")
	public Quokka deleteMedia(long mediaId) {
		recMediaService.deleteMedia(mediaId);
		return new Quokka(true);
	}

	/**
	 * 附件下载
	 * 
	 * @param files
	 * @return
	 */
	@GetMapping("download")
	public Quokka downloadMedia(long mediaId) {
		recMediaService.deleteMedia(mediaId);
		return new Quokka(true);
	}

	/**
	 * WORD PDF在线预览
	 * 
	 * @param files
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("showpdf")
	public Quokka showPdf(String path) throws IOException {
		InputStream in = MediaStorageHelper.download(path);
		byte[] file = IOUtils.toByteArray(in);
		if (!path.toLowerCase().endsWith("pdf")) {
			file = WordHelper.convertWord2Pdf(file);
		} else {
			file = IOUtils.toByteArray(in);
		}
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("pdfContent", Base64.getEncoder().encodeToString(file));
		return new Quokka(true, result);
	}

}
